package com.cqggrw.action;

import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cqggrw.domain.Book;
import com.cqggrw.domain.Json;
import com.cqggrw.service.BookServiceI;
import com.cqggrw.service.impl.BookServiceImpl;
import com.cqggrw.util.Utils;
import com.opensymphony.xwork2.ModelDriven;

public class BookAction extends BaseAction implements ModelDriven<Book> {

	private static final long serialVersionUID = 1L;
	
	private BookServiceI service = new BookServiceImpl();
	public Book book = new Book();
	
	public String add() {
		if(service.add(book, super.getProjectPath())) {
			super.addAttributeForRequest("message", "添加成功！");
			return "success";
		}
		super.addAttributeForRequest("message", "添加失败！");
		return "faile";
	}
	public String modify() {
		if(service.modify(book, super.getProjectPath())) {
			super.addAttributeForRequest("message", "修改成功！");
			return "success";
		}
		super.addAttributeForRequest("message", "修改失败！");
		return "faile";
	}
	
	public void getList() {
		super.writeJson(service.get(book));
	}

	public Book getModel() {
		return book;
	}
	
	public void delete() {
		Json json = new Json();
		if (service.del(book.getId())) {
			json.setSuccess(true);
			json.setMsg("删除成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("删除失败!");
		}

		super.writeJson(json);
	}
	
	public void downFile() {
		String filePath = super.getProjectPath()+Utils.separator+"images" +Utils.separator+"bookImg"+Utils.separator+book.getFmImgFileName();
		String fileName = book.getFmImgFileName()+".zip"; // 文件名，输出到用户的下载对话框
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setContentType("APPLICATION/OCTET-STREAM");
		resp.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
		try {
			Utils.zipFile(filePath, new ZipOutputStream(resp.getOutputStream()));
		} catch (Exception e) {
			Utils.log(e);
		}
	}

}
